package com.douwong.student.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.model.ClassMesageType;
import com.douwong.student.model.ClassMessage;

import java.util.ArrayList;

public class ClassMessageDBService extends BDBaseService<ClassMessage> {

//	private static ClassMessageDBService classMessageDBService=null;
	private SQLiteDatabase db ;
	public static ClassMessageDBService getInstance(Context context){
//		if(classMessageDBService==null){
//			classMessageDBService=new ClassMessageDBService(context);
//		}
		return new ClassMessageDBService(context);
	}
	protected ClassMessageDBService(Context context) {
		super(context);
	}
//messageType integer,identity integer,nickName text,creartTime text,filePath text,fileSize text,photoPath text
	//添加消息
	@Override
	public void add(ClassMessage message) {
		db=dbHelper.getWritableDatabase();
		switch (message.getMessageType()) {
	//文本消息
		case ClassMesageType.TEXT_MSG:
			db.execSQL("insert into class_message(messageType,identity,nickName,creartTime,photoPath,userId,content) values(?,?,?,?,?,?,?)", new Object[]{message.getMessageType(),message.getIdentity(),
					message.getNickName(),message.getCreateTime(),"",BaseApplication.sUserBean.getUserbean().getEuNumber(),message.getMessageContent()});
			
			break;
		//语音消息
		case ClassMesageType.VOICE_MSG:
			db.execSQL("insert into class_message(messageType,identity,nickName,creartTime,photoPath,userId,fileSize,filePath,isReaded,sendId) values(?,?,?,?,?,?,?,?,?,?)", new Object[]{message.getMessageType(),message.getIdentity(),
					message.getNickName(),message.getCreateTime(),"",BaseApplication.sUserBean.getUserbean().getEuNumber(),message.getVoiceTime(),message.getVoicePath(),(message.isReaded()==true? 1:0),message.getSendId()});
			break;
		//图片消息
		case ClassMesageType.IMAGE_MSG:
			Log.e("????????", "插入图片成功");
			db.execSQL("insert into class_message(messageType,identity,nickName,creartTime,photoPath,userId,filePath) values(?,?,?,?,?,?,?)", new Object[]{message.getMessageType(),message.getIdentity(),
					message.getNickName(),message.getCreateTime(),"",BaseApplication.sUserBean.getUserbean().getEuNumber(),message.getFilePath()});
			break;
		default:
			break;
		}
		db.close();
		
	}

	//删除消息
	@Override
	public void delete(int id) {
		db=dbHelper.getWritableDatabase();
		db.execSQL("delete from class_message where userId=?", new Object[]{id});
	}
	
	@Override
	public void update(int id) {
//		db=dbHelper.getWritableDatabase();
//		db.execSQL("update class_message set isReaded=1 where userId=? and id=?", new Object[]{MyApp.student.getUserId(),id});
//		db.close();
	}
public void upDate(String sendId) {
	db=dbHelper.getWritableDatabase();
	db.execSQL("update class_message set isReaded=1 where userId=? and sendId=?", new Object[]{BaseApplication.sUserBean.getUserbean().getEuNumber(),sendId});
	db.close();
}
	@Override
	public ClassMessage query(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	//id integer primary key autoincrement,messageType integer,identity integer,nickName text,creartTime text,filePath text,fileSize text,photoPath text,userId text
//查询消息集合
	public ArrayList<ClassMessage>query(ArrayList<ClassMessage>list){
		db=dbHelper.getWritableDatabase();
		Cursor cursor=db.rawQuery("select * from class_message where userId=?", new String[]{BaseApplication.sUserBean.getUserbean().getEuNumber()});
		while(cursor.moveToNext()){
			ClassMessage message=new ClassMessage();
			message.setIdentity(cursor.getInt(2));
			message.setNickName(cursor.getString(3));
			message.setCreateTime(cursor.getString(4));
			message.setSendId(cursor.getString(11));
			if(cursor.getInt(1)==ClassMesageType.TEXT_MSG){
				message.setMessageContent(cursor.getString(5));
				message.setMessageType(ClassMesageType.TEXT_MSG);
				list.add(message);
			}else if(cursor.getInt(1)==ClassMesageType.VOICE_MSG){
				message.setVoiceTime(cursor.getString(7));
				message.setVoicePath(cursor.getString(6));
				message.setReaded(cursor.getInt(10)==0? false:true);
				message.setMessageType(ClassMesageType.VOICE_MSG);
				list.add(message);
			}else if(cursor.getInt(1)==ClassMesageType.IMAGE_MSG){
				message.setMessageType(ClassMesageType.IMAGE_MSG);
				message.setFilePath(cursor.getString(6));
				list.add(message);
			}
			
		}
		cursor.close();
		db.close();
		return list;
		
	}
}
